package com.antkit.mark.ak;


import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;

import java.util.Locale;

//第一步，实现callBack
public class NfcP2pActivity extends AppCompatActivity implements NfcAdapter.CreateNdefMessageCallback{
    private NfcAdapter nfcAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);
        checkNfcBeam();
        //第二步，绑定callBack
        nfcAdapter.setNdefPushMessageCallback(this,this);
    }

    private void checkNfcBeam() {
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (nfcAdapter !=null){
            if (!nfcAdapter.isEnabled()){
                //支持NFC，但是没有打开
                Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                startActivity(intent);
            }else if(!nfcAdapter.isNdefPushEnabled()){
                //API 16+
                Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
                startActivity(intent);
            }
        }
    }

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        //封装数据，即写数据,发送数据
        ///NdefMessage ndefMessage=NfcWriteUtil.writeText("我是测试", Locale.CANADA,true);
      //  return ndefMessage;
        return null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        //接收数据
        setIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //判断数据是否为想要的
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
            //解析数据（读数据）
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
